﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Core;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.IO;
using Telerik.Web.Mvc;

namespace Sisocana.Controllers
{
    public class ReportesController : Controller
    {


        private IDonacionService _donacionService;
        private IPersonaService _personaService;
        
        public ReportesController()
            : this(new DonacionService(),new PersonaService())
        {

        }

        public ReportesController(IDonacionService donacionService ,IPersonaService personaService)
        {
            _donacionService = donacionService;
            _personaService = personaService;
        }

        
        //
        // GET: /Reportes/

        public ActionResult Index(int id)
        {
            ViewBag.id = id;
            ViewData["idSitio"] = id;
            return View();
        }

        

        public ActionResult RepDonacionBienesVieja(int id)
        {
            ViewData["idSitio"] = id;
            return View(_donacionService.GetAllDonacionBienes(c=>c.idSitio==id));
        }

        public ActionResult RepDonacionBienes(int? id, bool? ajax, bool? scrolling, bool? paging, bool? filtering, bool? sorting,
           bool? grouping, bool? showFooter)
        {

            ViewData["ajax"] = ajax ?? true;
            ViewData["scrolling"] = scrolling ?? true;
            ViewData["paging"] = paging ?? true;
            ViewData["filtering"] = filtering ?? true;
            ViewData["grouping"] = grouping ?? true;
            ViewData["sorting"] = sorting ?? true;
            ViewData["showFooter"] = showFooter ?? true;
            ViewData["idSitio"] = id;
            return View(_donacionService.GetAllDonacionBienes(c => c.idSitio == id));
        }


        [GridAction]
        public ActionResult _RepDonacionBienes(int? id)
        {
            ViewData["idSitio"] = id;           
            return View(new GridModel(_donacionService.GetAllDonacionBienes(c => c.idSitio == id)));
        }



        public ActionResult RepDonacionServicio(int? id, bool? ajax, bool? scrolling, bool? paging, bool? filtering, bool? sorting,
           bool? grouping, bool? showFooter)
        {

            ViewData["ajax"] = ajax ?? true;
            ViewData["scrolling"] = scrolling ?? true;
            ViewData["paging"] = paging ?? true;
            ViewData["filtering"] = filtering ?? true;
            ViewData["grouping"] = grouping ?? true;
            ViewData["sorting"] = sorting ?? true;
            ViewData["showFooter"] = showFooter ?? true;
            ViewData["idSitio"] = id;
            return View(_donacionService.GetAllDonacionServicios(c => c.idSitio == id));
        }


        [GridAction]
        public ActionResult _RepDonacionServicio(int id)
        {
            ViewData["idSitio"] = id;
            return View(new GridModel(_donacionService.GetAllDonacionServicios(c => c.idSitio == id)));
        }


        public ActionResult RepDonacionEconomica(int? id, bool? ajax, bool? scrolling, bool? paging, bool? filtering, bool? sorting,
           bool? grouping, bool? showFooter)
        {

            ViewData["ajax"] = ajax ?? true;
            ViewData["scrolling"] = scrolling ?? true;
            ViewData["paging"] = paging ?? true;
            ViewData["filtering"] = filtering ?? true;
            ViewData["grouping"] = grouping ?? true;
            ViewData["sorting"] = sorting ?? true;
            ViewData["showFooter"] = showFooter ?? true;
            ViewData["idSitio"] = id;
            return View(_donacionService.GetAllDonacionEconomica(c => c.idSitio == id));
        }


        [GridAction]
        public ActionResult _RepDonacionEconomica(int id)
        {
            ViewData["idSitio"] = id;
            return View(new GridModel(_donacionService.GetAllDonacionEconomica(c => c.idSitio == id)));
        }


        public ActionResult RepPedAyuda(int? id, bool? ajax, bool? scrolling, bool? paging, bool? filtering, bool? sorting,
           bool? grouping, bool? showFooter)
        {

            ViewData["ajax"] = ajax ?? true;
            ViewData["scrolling"] = scrolling ?? true;
            ViewData["paging"] = paging ?? true;
            ViewData["filtering"] = filtering ?? true;
            ViewData["grouping"] = grouping ?? true;
            ViewData["sorting"] = sorting ?? true;
            ViewData["showFooter"] = showFooter ?? true;
            ViewData["idSitio"] = id;
            return View(_personaService.GetAllPersonaAyudaModel(c => c.idSitio == id));
        }


        [GridAction]
        public ActionResult _RepPedAyuda(int id)
        {
            ViewData["idSitio"] = id;
            return View(new GridModel(_personaService.GetAllPersonaAyudaModel(c => c.idSitio == id)));
        }

        


        public ActionResult GetChart(int id,int tipo,string titulo,string ext)
        {
            var listVal = new List<int>();
            int acu = 0;
            string titEje = "";

            switch (tipo)
            {
                case 1:
                    var lista = _donacionService.GetAllDonacionEconomica(c => c.idSitio == id);
                    var max = lista.Max(c => c.fecha);
                    var min = lista.Min(c => c.fecha);

                    while (max.Date>=min.Date)
                    {

                        var listaAux = lista.Where(c => c.fecha.Date == min.Date);
                        acu=0;
                        foreach (var c in listaAux)
                        {
                            acu += c.monto;                           
                        }
                        listVal.Add(acu);
                        min=min.AddDays(1);
                        
                    }
                    titEje = "Financial donations amount";
                    break;
                case 2:
                    var lista2 = _donacionService.GetAllDonacionBienes(c => c.idSitio == id);
                    var max2 = lista2.Max(c => c.fecha);
                    var min2 = lista2.Min(c => c.fecha);

                    while (max2.Date >= min2.Date)
                    {

                        var listaAux = lista2.Where(c => c.fecha.Date == min2.Date);
                        acu=0;
                        foreach (var c in listaAux)
                        {
                            acu += c.cantidadDelBien;                           
                        }
                        listVal.Add(acu);
                        min2=min2.AddDays(1);
                        
                    }

                    titEje = "Donations of goods amount";
                    
                    break;
                case 3:
                    var lista3 = _donacionService.GetAllDonacionServicios(c => c.idSitio == id);
                    var max3 = lista3.Max(c => c.fecha);
                    var min3 = lista3.Min(c => c.fecha);

                    while (max3.Date >= min3.Date)
                    {

                        var listaAux = lista3.Where(c => c.fecha.Date == min3.Date);
                        acu=0;
                        foreach (var c in listaAux)
                        {
                            acu += 1;                           
                        }
                        listVal.Add(acu);
                        min3=min3.AddDays(1);
                        
                    }
                    titEje = "Donations of services Amount";
                    break;
                case 4:
                    var lista4 = _personaService.GetAllPersonaAyudaModel(c => c.idSitio == id);
                   var max4 = lista4.Max(c => c.fecha);
                    var min4 = lista4.Min(c => c.fecha);

                    while (max4.Date >= min4.Date)
                    {

                        var listaAux = lista4.Where(c => c.fecha.Date == min4.Date);
                        acu=0;
                        foreach (var c in listaAux)
                        {
                            acu += 1;                           
                        }
                        listVal.Add(acu);
                        min4=min4.AddDays(1);
                        
                    }
                    titEje = "Number of Request for Assistance";
                    break;
            }

                /*ANTES LO PASABA POR PARAMETRO QUEDO ASI PARA NO TOCAR NDA*/

                  ViewData["idSitio"] = id;
                  ViewData["Chart"] = listVal; 
               
                                  
                  Chart Chart2 = new System.Web.UI.DataVisualization.Charting.Chart();  
                  Chart2.Width = 700;  
                  Chart2.Height = 500;  
                  Chart2.RenderType = RenderType.ImageTag;    
                  Chart2.Palette = ChartColorPalette.BrightPastel;  
                  Title t = new Title(titulo, Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));  
                  Chart2.Titles.Add(t);  
                  Chart2.ChartAreas.Add("Series 1");

                  Chart2.ChartAreas.FirstOrDefault().Area3DStyle.Enable3D = true;
                  Chart2.ChartAreas.FirstOrDefault().AxisX.LabelStyle.Enabled=false;
                  Chart2.ChartAreas.FirstOrDefault().AxisX.Title = "Time in days";
                  Chart2.ChartAreas.FirstOrDefault().AxisY.Title = titEje;

                  Chart2.Series.Add("Series 1");  
                  
                  Chart2.Series["Series 1"].ChartType = SeriesChartType.Spline;


                  foreach (int value in (List<int>)ViewData["Chart"])  
                  {  
                      Chart2.Series["Series 1"].Points.AddY(value);  
                  }  
  
                
  
                  Chart2.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;  
                  Chart2.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);  
                  Chart2.BorderlineDashStyle = ChartDashStyle.Solid;  
                  Chart2.BorderWidth = 2;  
  
                //  Chart2.Legends.Add("Legend1");  
                  var memoryStream = new MemoryStream();
                  Chart2.SaveImage(memoryStream);
            
                  return File(memoryStream.GetBuffer(), @"image/png");
        }


    }


    
}
